package com.rt.system.service.impl;

import java.util.List;
import com.rt.common.utils.DateUtils;
import com.rt.common.utils.StringUtils;
import com.rt.system.domain.RtImportanceInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.rt.system.mapper.RtKnowledgeInfoMapper;
import com.rt.system.domain.RtKnowledgeInfo;
import com.rt.system.service.IRtKnowledgeInfoService;

/**
 * 大纲Service业务层处理
 * 
 * @author Yg
 * @date 2022-04-13
 */
@Service
public class RtKnowledgeInfoServiceImpl implements IRtKnowledgeInfoService 
{
    @Autowired
    private RtKnowledgeInfoMapper rtKnowledgeInfoMapper;

    /**
     * 查询大纲
     * 
     * @param id 大纲主键
     * @return 大纲
     */
    @Override
    public RtKnowledgeInfo selectRtKnowledgeInfoById(Long id)
    {
        return rtKnowledgeInfoMapper.selectRtKnowledgeInfoById(id);
    }

    /**
     * 查询大纲列表
     * 
     * @param rtKnowledgeInfo 大纲
     * @return 大纲
     */
    @Override
    public List<RtKnowledgeInfo> selectRtKnowledgeInfoList(RtKnowledgeInfo rtKnowledgeInfo)
    {
        return rtKnowledgeInfoMapper.selectRtKnowledgeInfoList(rtKnowledgeInfo);
    }

    /**
     * 新增大纲
     * 
     * @param rtKnowledgeInfo 大纲
     * @return 结果
     */
    @Override
    public int insertRtKnowledgeInfo(RtKnowledgeInfo rtKnowledgeInfo)
    {
        rtKnowledgeInfo.setCreateTime(DateUtils.getNowDate());
        return rtKnowledgeInfoMapper.insertRtKnowledgeInfo(rtKnowledgeInfo);
    }

    /**
     * 修改大纲
     * 
     * @param rtKnowledgeInfo 大纲
     * @return 结果
     */
    @Override
    public int updateRtKnowledgeInfo(RtKnowledgeInfo rtKnowledgeInfo)
    {
        rtKnowledgeInfo.setUpdateTime(DateUtils.getNowDate());
        return rtKnowledgeInfoMapper.updateRtKnowledgeInfo(rtKnowledgeInfo);
    }

    /**
     * 批量删除大纲
     * 
     * @param ids 需要删除的大纲主键
     * @return 结果
     */
    @Override
    public int deleteRtKnowledgeInfoByIds(Long[] ids)
    {
        return rtKnowledgeInfoMapper.deleteRtKnowledgeInfoByIds(ids);
    }

    /**
     * 删除大纲信息
     * 
     * @param id 大纲主键
     * @return 结果
     */
    @Override
    public int deleteRtKnowledgeInfoById(Long   id)
    {
        // 大纲
        RtKnowledgeInfo rtKnowledgeInfo = rtKnowledgeInfoMapper.selectRtKnowledgeInfoById(id);
        if(rtKnowledgeInfo.getType()==1){
            // 查询是否有节
            RtKnowledgeInfo rtKnowledgeInfo1 = new RtKnowledgeInfo();
            rtKnowledgeInfo1.setParentId(String.valueOf(rtKnowledgeInfo.getId()));
            List<RtKnowledgeInfo> rtKnowledgeInfos = rtKnowledgeInfoMapper.selectRtKnowledgeInfoList(rtKnowledgeInfo1);
            if (StringUtils.isNotNull(rtKnowledgeInfos)){
                for (RtKnowledgeInfo knowledgeInfo : rtKnowledgeInfos) {
                    // 先删除知识点
                    rtKnowledgeInfoMapper.deleteRtKnowledgeInfoByPId(knowledgeInfo.getId());
                }
            }
            // 删除节点
            rtKnowledgeInfoMapper.deleteRtKnowledgeInfoByPId(rtKnowledgeInfo.getId());
            // 删除大纲
            return rtKnowledgeInfoMapper.deleteRtKnowledgeInfoById(id);
        }else if(rtKnowledgeInfo.getType()==2){
            rtKnowledgeInfoMapper.deleteRtKnowledgeInfoByPId(rtKnowledgeInfo.getId());
            return rtKnowledgeInfoMapper.deleteRtKnowledgeInfoById(id);
        }
        return rtKnowledgeInfoMapper.deleteRtKnowledgeInfoById(id);
    }

    @Override
    public List<RtImportanceInfo> selectRtImportanceInfoList(RtImportanceInfo rtImportanceInfo) {
        return rtKnowledgeInfoMapper.selectRtImportanceInfoList(rtImportanceInfo);
    }

    @Override
    public String selectRtKnowledgeInfoByName(RtKnowledgeInfo rtKnowledgeInfo) {
        return rtKnowledgeInfoMapper.selectRtKnowledgeInfoByName(rtKnowledgeInfo);
    }
}
